import {
  getFamilyMemberApi,
  inviteRecordApi,
  bindingFamilyMember,
  getPersonInfoApi,
  modifyFamilyMemberApi
} from "../../../services/filing/filing"
import {
  GETSTORAGE,
  SETSTORAGE
} from "../../../services/storage/storage"

import Message from '../../../miniprogram_npm/tdesign-miniprogram/message/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeValues: [0],
    nameValue: '',
    nameError: false,
    sexValue: '点击选择性别',
    addressValue: '',
    IDValue: '',
    ageValue: '',
    familyMemberId: '',
    phoneNumber: '',
    citys: [{
        label: '男',
        value: '男'
      },
      {
        label: '女',
        value: '女'
      },
    ],
    invitationPopup: false,
    phoneError: false,
    phoneNumber: '',
    relationShip: ''
  },
  handleChange(e) {
    this.setData({
      activeValues: e.detail.value,
    });
  },
  //年龄
  onAgeInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      ageValue: value
    })
  },
  // 身份证号
  onIDInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      IDValue: value
    })
  },
  // 姓名输入框
  onNameInput(e) {
    const {
      value
    } = e.detail;
    if (value == '') {
      this.setData({
        nameError: true,
        nameValue: value
      })
    } else {
      this.setData({
        nameError: false,
        nameValue: value
      })
    }
  },
  // 手机号输入
  onPhoneInput(e) {
    const {
      phoneError
    } = this.data;
    const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
    if (!e.detail.value) {
      this.setData({
        phoneError: false,
        phoneNumber: e.detail.value
      })
      return
    }
    if (phoneError === isPhoneNumber) {
      this.setData({
        phoneError: !isPhoneNumber,
        phoneNumber: e.detail.value
      });

    }
  },

  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      sexValue: value,
      [`${key}Text`]: value.join(' '),
    });
  },
  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },

  onWithoutTitlePicker() {
    this.setData({
      city2Visible: true,
      city2Title: ''
    });
  },
  showPopup() {
    if (!this.data.phoneError && !this.data.nameError && this.data.phoneNumber != '' && this.data.nameValue != '') {
      this.setData({
        invitationPopup: true
      })
    } else {
      if (this.data.phoneError || this.data.nameError) {
        Message.error({
          context: this,
          offset: [20, 32],
          duration: 2000,
          content: '输入内容不正确',
        });
        return
      } else {
        Message.error({
          context: this,
          offset: [20, 32],
          duration: 2000,
          content: '输入框不能为空',
        });
        return
      }
    }


  },

  closePopup() {
    this.setData({
      invitationPopup: false
    })
  },
  // 地址
  onAddressInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      addressValue: value
    })
  },
  // 邀请
  inviteRecord() {
    const userId = GETSTORAGE('userId')
    const inviteFamilyId = GETSTORAGE('inviteFamilyId')
    let obj = {
      familyMemberId: this.data.familyMemberId,
      userId: userId,
      nickName: this.data.nameValue,
      sex: this.data.sexValue[0],
      mobilePhoneNumber: this.data.phoneNumber,
      age: this.data.ageValue,
      address: this.data.addressValue,
    }
    let modifyObj = {
      userId: userId,
      familyMemberId: this.data.familyMemberId,
      familyId: inviteFamilyId,
      nickName: this.data.nameValue,
      sex: this.data.sexValue[0],
      relationShip: this.data.relationShip
    }

    getPersonInfoApi({
      userId: userId
    }).then(res => {
      console.log(res, '-------');
      if (res.data.data != null) {
        console.log('绑定');
        bindingFamilyMember({
          familyMemberId: this.data.familyMemberId,
          personId: res.data.data.personId
        }).then(res => {
          console.log(res, '11111111');
          modifyFamilyMemberApi(modifyObj).then(res => {
            console.log(modifyObj);
            console.log(res, '修改家庭成员信息');
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        })
      } else {
        inviteRecordApi(obj).then(res => {
          console.log("邀请建档", res);
          SETSTORAGE('personId', res.data.data.personId)
          SETSTORAGE('familyMemberId', res.data.data.familyMemberId)
          SETSTORAGE('familyId', res.data.data.familyId)
          wx.switchTab({
            url: '/pages/index/index',
          })
        })
      }
      this.setData({
        invitationPopup: false
      })
    })

  },
  getFamilyMemberInfo() {
    getFamilyMemberApi({
      familyId: '',
      familyMemberId: this.data.familyMemberId
    }).then(res => {
      if (res.data.data == 'null' || res.data.code == 50000) return
      const list = res.data.data.filter(item => item.familyMemberId == this.data.familyMemberId)
      console.log(list, '-------------list')
      this.setData({
        nameValue: list[0].nickName,
        sexValue: list[0].sex,
        relationShip: list[0].relationShipId
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      familyMemberId: options.familyMemberId
    })
    this.getFamilyMemberInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getFamilyMemberInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})